
window.onload=function(){
	var str=$("#proTypes").val();		
	stri=str.substring(0,str.length-1);
	stri1=stri.split(",");
	if(stri1 != ""){
		for(var i=0;i<stri1.length;i++){
			$("#ss").after("<tr><td></td><td><div id='ccc'>"+stri1[i]+"<a onclick='del(this)' href='javascript:;'>×</a></div></td></tr>");	
		}
	}
}

function ajaxFileUpload(u,uu)
{
	var imageFile = document.getElementById(u).value;
	
	var isImage = picIsRule(imageFile,true);
	if(isImage){}else{
			return false;
		}

	var id = "merchant";
	$.ajaxFileUpload
	(
		{
			url:path+'/manager_merchant/ajax_img?id=merchant',
			secureuri:false,
			fileElementId:u,
			dataType: 'json',
			success: function (data, status)
			{	
			
				if(data.msg=="成功"){
					//layer.alert(data.targetFileName);
					document.getElementById(uu).value=data.targetFileName;
					var src=imagePath+"/upload/"+id+"/"+data.targetFileName;
					var ids="#"+u+"1";
					$(ids).attr("src",src);
					layer.alert(data.msg);
					DrawImage(document.getElementById(u+"1"),300,250);
					
				}else{
					layer.alert(data.msg);
				}
			},
			error: function (data, status, e)
			{
				layer.alert("上传失败！大小不能超过5M");
			}
		}
	);
	
	return false;

} 
function checkMerchant(){
		var loginName = $("#loginName").val();
		if(loginName.length <0 ){
				layer.alert("请填写登陆帐号！");
				return false;
		}
		if(!isChinaOrNumbOrLett(loginName)|| loginName.length<4 || loginName.length>12){
				layer.alert("请填写正确的登陆帐号，4-12位只由汉字、字母、数字组成");
				return false;
		}
			$.ajax({   
		           	url: path+"/manager_merchant/check_merchant",			
					type:"post",
					dataType:"html",
					data:{"merchantLoginName":loginName},
					beforeSend:function(){
						   
					},
					error:function(XMLHttpRequest, textStatus, errorThrown){
			         layer.alert("出错啦！");
					},
					success:function(msg) 
					{
				    	$("#msg").empty();
				    	if(msg=="ok"){
				    		flag = true;
				    		$("#msg").html("<span style='color:green'>恭喜您该帐号可以使用!</span>");
				    	}else{
				    		$("#loginName").focus();
				    		$("#msg").html("<span style='color:red'>对不起，该帐号已被占用!</span>");
				    	}
		           	}
			});
}

function sub_form(){
	    proTypesToStr();
	    var mercName=trim(document.getElementById('mercName').value);
		var pas=document.getElementById('pas').value;//商户密码
		var loginName = $("#loginName").val();//商户平台登陆帐号
		var margin=trim(document.getElementById('margin').value); 		
		var mercType =$("#Type").val();//下拉
		var aptitudeImage=trim(document.getElementById('mainImage').value);
		var firstImage=trim(document.getElementById('firstImage').value);
		var secondImage=trim(document.getElementById('secondImage').value);
		var thirdImage=trim(document.getElementById('thirdImage').value);
		var fourthImage=trim(document.getElementById('fourthImage').value);
		var fifthImage=trim(document.getElementById('fifthImage').value);
		var sixthImage=trim(document.getElementById('sixthImage').value);
		var corporateRepre=trim(document.getElementById('corporateRepre').value);
		var linkMan=trim(document.getElementById('linkMan').value);
		var linkTel=trim(document.getElementById('linkTel').value);
		var address=trim(document.getElementById('address').value);
		var phone=trim(document.getElementById('phone').value);
		var email=trim(document.getElementById('email').value);
		var collAgreement=trim(document.getElementById('collAgreement').value);
		var proTypes=document.getElementById('proTypes').value;
		var detail=document.getElementById('detail').value;
		var aptitudeName=trim(document.getElementById('aptitudeName').value);
		var firstName=trim(document.getElementById('firstName').value);
		var secondName=trim(document.getElementById('secondName').value);
		var thirdName=trim(document.getElementById('thirdName').value);
		var fourthName=trim(document.getElementById('fourthName').value);
		var fifthName=trim(document.getElementById('fifthName').value);
		var sixthName=trim(document.getElementById('sixthName').value);
			var pattern1 =/(^[\u4e00-\u9fa5A-Za-z0-9]*$)/;
			var pattern2 =/(^[0-9]*$)/;
			var pattern3 =/(^[A-Za-z0-9]*$)/;
			var pattern4 =/(^[\u4e00-\u9fa5A-Za-z0-9_-]*$)/;
			if(mercName.length == 0){
				layer.alert("供应商名不能为空且不能全部为空格");
				return false;
			}
			if(!pattern1.test(document.getElementById('mercName').value)){
				layer.alert("供应商名称只能为数字，字母和汉字组成");
				return false;
			}
			if(pattern2.test(document.getElementById('mercName').value)){
				layer.alert("供应商名称不能全部为数字");
				return false;
			}
			if(loginName.length <0 ){
				layer.alert("请填写登陆帐号！");
				return false;
			}
			if(!isChinaOrNumbOrLett(loginName)|| loginName.length<4 || loginName.length>12){
				layer.alert("请填写正确的登陆帐号，4-12位只由汉字、字母、数字组成");
				return false;
			}
			if(pas.length>0 || !isEdit){
			if (pas.length > 1 && pas.replace(/(^\s*)|(\s*$)/g,"")=="") {
				layer.alert("登录密码不能为空格");
				return false;
			}
			if(!pattern3.test(pas)){
				layer.alert("登录密码只能输入字母与数字");
				return false;
			}
			if(pas.length < 6  && pas.length > 1){
				layer.alert("登录密码只能为6-12位");
				return false;
			}
			if(null!=easy[pas]){
			  	layer.alert("密码安全程度较低，请使用字母和数字组合！");
	            return false;
			}
			if(!isNaN(pas)){
			  	layer.alert("密码必须包含字母和数字！");
	            return false;
			} 
			if(pas.length > 12){
				layer.alert("登录密码只能为6-12位");
				return false;
			}
			}
			if(!pattern2.test(margin)){
				layer.alert("保证金只能输入数字");
				return false;
			}
			if(corporateRepre.length == 0){
				layer.alert("法人不能为空或者不能全部为空格");
				return false;
			}
			if(!pattern1.test(document.getElementById('corporateRepre').value)){
				layer.alert("法人只能为字母和中文");
				return false;
			}
			if(linkMan.length == 0){
				layer.alert("联系人不能为或者全为空格");
				return false;
			}
			if(!pattern1.test(document.getElementById('linkMan').value)){
				layer.alert("联系人只能为字母和中文组成");
			}
			if(linkTel.lenght == 0){
				layer.alert("电话号码不能为空或者全为空格");
				return false;
			}
			if(!IsTelephone(linkTel))
			{
				layer.alert("电话号码格式不正确,为区号-电话号码")
				return false;
			}
			if(phone.length == 0){
				layer.alert("手机不能为空且不能全为空格");
				return false;
			}
			if(!checkPhone(phone)){
				layer.alert("请输入正确的手机号码！")
				return false;
			}
			if(phone.length != 11){
				layer.alert("手机号码长度必须为11位");
				return false;
			}
			if(email.length == 0){
				layer.alert("邮箱不能为空且不能全部为空格");
				return false;
			}
			
			if(!verifyEmail(email)){
				layer.alert("请输入正确的邮箱格式")
				return false;
			}
			if(address.length == 0){
				layer.alert("地址输入不能为空且不能全部为空格");
				return false;
			}
			if(!pattern4.test(address)){
				layer.alert("地址只能输入字母，汉字和数字");
				return false;
			}
			if(pattern2.test(address)){
				layer.alert("地址不能为纯数字");
				return false;
			}
			if(collAgreement.length == 0){
				layer.alert("合作协议不能为空且不能全部为空格");
				return false;
			}
			if(pattern2.test(collAgreement.lenght)){
				layer.alert("合作协议不能为纯数字");
				return false;
			}
			if(detail.length > 250){
				layer.alert("商户简介只能输入250个字符");
				return false;
			}
	var layer_load_window = layer.load(2); 
	$("#save_form").ajaxSubmit({
		success:function(data){
			if(data == 'success'){
				layer.msg('操作成功!',{icon:1,time:1000},function(){
					var index = parent.layer.getFrameIndex(window.name);
					parent.search_form.submit();
					parent.layer.close(index);
				});
			} else if (data == 'fail') {
				layer.alert("保存失败!");
			} else{
				layer.alert(data);
			}
		},
		error:function(){
		layer.alert("保存失败!");
		},complete: function(XMLHttpRequest, textStatus) {
			layer.close(layer_load_window);
		}
	});
}
function findType(e){
	var parantid=document.getElementById(e).value;
	var url=path+"/manager_merchant/find_child_type";
	var sid=e+"1";
	if(parantid!=""){
		$.getJSON(url,{parantId:parantid},function(msg){
					if(msg.length!=0){
						document.getElementById(sid).options.length=1;
						for(var i=0;i<msg.length;i++){
							var option=new Option(msg[i].name,msg[i].id);
							document.getElementById(sid).options.add(option);	
						}
					}else{
					   document.getElementById(sid).options.length=1;
					   $("#Type11 option[value='']").attr("selected","true");
						layer.alert("该商品分类下没有分类，请去商品类型模块添加！");
					}
				});
		}else{
			document.getElementById(sid).options.length=1;
			$("#Type11 option[value='']").attr("selected","true");
		}
}
$("#add").click(function(){
	var ty=$("#Type1").val();
	var ty2=$("#Type11").val();
	if(ty!=''&&ty2!=''){
		var str=$("#Type1").find("option:selected").text()+"   "+$("#Type11").find("option:selected").text();
		$("#ss").after("<tr><td></td><td><div id='ccc'>"+str+"<a onclick='del(this)' href='javascript:;'>×</a></div></td></tr>");	
	}else{
		layer.alert("请明细分类！")
	}
});
function del(e){
	$(e).parents("tr").remove();
}
function proTypesToStr(){
	var str='';
	$("div[id='ccc']").each(function(){
			str+=($(this).text()+',').replace("×","");				
	})
	document.getElementById("proTypes").value=str;
}
function CheckLength(e) 
{ 
   	var ValidStrLength=500; 
	var lens=$(e).val().length;
	var View=parseInt((ValidStrLength-lens)/2);
	if((ValidStrLength-lens)>=0){
		var str="<span id='sd'>您还可以输入"+View+"字</span>";
		$("#sd").replaceWith(str);
	}else{
		$(e).val("");
		layer.alert("超过最大长度，请重输")
	}
}//文本框长度设置
// 电话正则判断
function IsTelephone(obj)
{ 
	var pattern=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{3,4}\-[0-9]{7,8}\-[0-9]{3,8}$)/; 
	if(pattern.test(obj)) 
	{ 
		return true; 
	} else { 
		return false; 
	} 
}	
//检查email邮箱
function verifyEmail(emailAddr){
	var reg = /^([a-zA-Z0-9_\.-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
	if (reg.test(emailAddr)){
		return true;
	} else {
		return false;		
	}
} 
//验证手机
function checkPhone(mobilPhone){
	var isMobilPhone = /^1[3,4,5,8]\d{9}$/.test(mobilPhone);
	if(isMobilPhone){
		return true;
	}else{
		return false;	
	}
}
function cancle() {
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
function trim(str) {
	return (str + "").replace(/^\s+|\s+$/g, "");
}